const Koa = require('koa');
const {responseErr, responseSuc} = require("../utils/app/response");
const fileService = require("../services/fileService");
const app = new Koa();

// 自定义中间件
const checkChunkMiddleware = async (ctx, next) => {
    const file_hash = ctx.get("fileHash")
    const chunk_index = ctx.get("chunkIndex")
    if (!file_hash || !chunk_index) {
        responseErr({ctx: ctx, msg: "chunk_index和file_hash 必填"})
        return
    }
    let checkSlice= await fileService.checkSlice({file_hash, chunk_index})
    if (checkSlice.err){
        responseErr({ctx: ctx, msg: checkSlice.err})
        return
    }
    if (checkSlice.data.isHas){
        responseSuc({ctx: ctx})
        return
    }
    await next();  // 调用 next() 将请求传递给下一个中间件

};

module.exports=checkChunkMiddleware
